package com.rla.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.rla.common.core.page.TableDataInfo;
import com.rla.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rla.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.rla.domain.bo.BaseNewsInformationBo;
import com.rla.domain.vo.BaseNewsInformationVo;
import com.rla.domain.BaseNewsInformation;
import com.rla.mapper.BaseNewsInformationMapper;
import com.rla.service.IBaseNewsInformationService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 健康科普Service业务层处理
 *
 * @author rla
 * @date 2025-08-02
 */
@RequiredArgsConstructor
@Service
public class BaseNewsInformationServiceImpl implements IBaseNewsInformationService {

    private final BaseNewsInformationMapper baseMapper;

    /**
     * 查询健康科普
     */
    @Override
    public BaseNewsInformationVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询健康科普列表
     */
    @Override
    public TableDataInfo<BaseNewsInformationVo> queryPageList(BaseNewsInformationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaseNewsInformation> lqw = buildQueryWrapper(bo);
        Page<BaseNewsInformationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询健康科普列表
     */
    @Override
    public List<BaseNewsInformationVo> queryList(BaseNewsInformationBo bo) {
        LambdaQueryWrapper<BaseNewsInformation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaseNewsInformation> buildQueryWrapper(BaseNewsInformationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaseNewsInformation> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), BaseNewsInformation::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getClassification()), BaseNewsInformation::getClassification, bo.getClassification());
        lqw.eq(StringUtils.isNotBlank(bo.getAuthor()), BaseNewsInformation::getAuthor, bo.getAuthor());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), BaseNewsInformation::getImage, bo.getImage());
        lqw.eq(StringUtils.isNotBlank(bo.getInfo()), BaseNewsInformation::getInfo, bo.getInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getStatement()), BaseNewsInformation::getStatement, bo.getStatement());
        lqw.eq(bo.getSort() != null, BaseNewsInformation::getSort, bo.getSort());
        lqw.eq(StringUtils.isNotBlank(bo.getIsShow()), BaseNewsInformation::getIsShow, bo.getIsShow());
        lqw.eq(StringUtils.isNotBlank(bo.getIsTop()), BaseNewsInformation::getIsTop, bo.getIsTop());
        lqw.eq(StringUtils.isNotBlank(bo.getNewDate()), BaseNewsInformation::getNewDate, bo.getNewDate());
        lqw.orderByAsc(BaseNewsInformation::getSort);
        return lqw;
    }

    /**
     * 新增健康科普
     */
    @Override
    public Boolean insertByBo(BaseNewsInformationBo bo) {
        BaseNewsInformation add = BeanUtil.toBean(bo, BaseNewsInformation.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改健康科普
     */
    @Override
    public Boolean updateByBo(BaseNewsInformationBo bo) {
        BaseNewsInformation update = BeanUtil.toBean(bo, BaseNewsInformation.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaseNewsInformation entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除健康科普
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
